package cn.gcyl.module.biz.controller.admin.alliancemembers;

import cn.gcyl.module.biz.controller.admin.platforminformation.vo.PlatformInformationPageReqVO;
import cn.gcyl.module.biz.dal.dataobject.platforminformation.PlatformInformationDO;
import cn.gcyl.module.biz.service.platforminformation.PlatformInformationService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.gcyl.framework.common.pojo.PageParam;
import cn.gcyl.framework.common.pojo.PageResult;
import cn.gcyl.framework.common.pojo.CommonResult;
import cn.gcyl.framework.common.util.object.BeanUtils;
import static cn.gcyl.framework.common.pojo.CommonResult.success;

import cn.gcyl.framework.excel.core.util.ExcelUtils;

import cn.gcyl.framework.apilog.core.annotation.ApiAccessLog;
import static cn.gcyl.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.gcyl.module.biz.controller.admin.alliancemembers.vo.*;
import cn.gcyl.module.biz.dal.dataobject.alliancemembers.AllianceMembersDO;
import cn.gcyl.module.biz.service.alliancemembers.AllianceMembersService;

@Tag(name = "管理后台 - 联盟成员链接")
@RestController
@RequestMapping("/biz/alliance-members")
@Validated
public class AllianceMembersController {

    @Resource
    private AllianceMembersService allianceMembersService;
    @Resource
    private PlatformInformationService platformInformationService;

    @PostMapping("/create")
    @Operation(summary = "创建联盟成员链接")
//    @PreAuthorize("@ss.hasPermission('biz:alliance-members:create')")
    public CommonResult<Integer> createAllianceMembers(@Valid @RequestBody AllianceMembersSaveReqVO createReqVO) {
        return success(allianceMembersService.createAllianceMembers(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新联盟成员链接")
//    @PreAuthorize("@ss.hasPermission('biz:alliance-members:update')")
    public CommonResult<Boolean> updateAllianceMembers(@Valid @RequestBody AllianceMembersSaveReqVO updateReqVO) {
        allianceMembersService.updateAllianceMembers(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除联盟成员链接")
    @Parameter(name = "id", description = "编号", required = true)
//    @PreAuthorize("@ss.hasPermission('biz:alliance-members:delete')")
    public CommonResult<Boolean> deleteAllianceMembers(@RequestParam("id") Integer id) {
        allianceMembersService.deleteAllianceMembers(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除联盟成员链接")
//                @PreAuthorize("@ss.hasPermission('biz:alliance-members:delete')")
    public CommonResult<Boolean> deleteAllianceMembersList(@RequestParam("ids") List<Integer> ids) {
        allianceMembersService.deleteAllianceMembersListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得联盟成员链接")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
//    @PreAuthorize("@ss.hasPermission('biz:alliance-members:query')")
    public CommonResult<AllianceMembersRespVO> getAllianceMembers(@RequestParam("id") Integer id) {
        AllianceMembersDO allianceMembers = allianceMembersService.getAllianceMembers(id);
        return success(BeanUtils.toBean(allianceMembers, AllianceMembersRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得联盟成员链接分页")
//    @PreAuthorize("@ss.hasPermission('biz:alliance-members:query')")
    public CommonResult<PageResult<AllianceMembersRespVO>> getAllianceMembersPage(@Valid AllianceMembersPageReqVO pageReqVO) {
        PageResult<AllianceMembersDO> pageResult = allianceMembersService.getAllianceMembersPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AllianceMembersRespVO.class));
    }
    @GetMapping("/getAllianceMembersInfoPage")
    @Operation(summary = "获得联盟成员链接信息列表")
//    @PreAuthorize("@ss.hasPermission('biz:alliance-members:query')")
    public CommonResult<PageResult<PlatformInformationDO>> getAllianceMembersInfoPage(@Valid PlatformInformationPageReqVO pageReqVO) {
        PageResult<PlatformInformationDO> pageResult = allianceMembersService.getAllianceMembersInfoPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, PlatformInformationDO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出联盟成员链接 Excel")
//    @PreAuthorize("@ss.hasPermission('biz:alliance-members:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportAllianceMembersExcel(@Valid AllianceMembersPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<AllianceMembersDO> list = allianceMembersService.getAllianceMembersPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "联盟成员链接.xls", "数据", AllianceMembersRespVO.class,
                        BeanUtils.toBean(list, AllianceMembersRespVO.class));
    }

}